﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Syndication;
using System.Xml.Linq;
using NCMIS.ObjectModel.MetaData;

namespace NCMIS.ServiceModel.Syndication
{
    /// <summary>
    /// Represents the standard CMIS service document collections.
    /// </summary>
    public enum ServiceDocumentCollection
    {
        RootFolder,
        TypesChildren,
        CheckedOut,
        Unfiled,
        Query
    }

    public static class ServiceDocumentExtensions
    {
        /// <summary>
        /// Returns the type name of the specified collection.
        /// </summary>
        /// <param name="collection">The collection whose type name is to be retrieved</param>
        /// <returns>The collection type name</returns>
        private static string GetCollectionTypeName(ServiceDocumentCollection collection)
        {
            string result = null;
            switch (collection)
            {
                case ServiceDocumentCollection.RootFolder:
                    result = "root";
                    break;
                case ServiceDocumentCollection.TypesChildren:
                    result = "types";
                    break;
                case ServiceDocumentCollection.CheckedOut:
                    result = "checkedout";
                    break;
                case ServiceDocumentCollection.Unfiled:
                    result = "unfiled";
                    break;
                case ServiceDocumentCollection.Query:
                    result = "query";
                    break;
            }
            return result;
        }

        /// <summary>
        /// Returns a collection from the specified service document.
        /// </summary>
        /// <param name="serviceDocument">The service document that contains the collection</param>
        /// <param name="collectionType">The type of the collection</param>
        /// <returns></returns>
        public static ResourceCollectionInfo GetCollection(this ServiceDocument serviceDocument, ServiceDocumentCollection collectionType)
        {
            ResourceCollectionInfo result = null;
            if (serviceDocument.Workspaces != null)
            {
                Workspace workspace = serviceDocument.Workspaces[0];
                foreach (ResourceCollectionInfo collection in workspace.Collections)
                {
                    foreach (SyndicationElementExtension extension in collection.ElementExtensions.Where<SyndicationElementExtension>(x => x.OuterName == "collectionType"))
                    {
                        XElement xelement = XElement.ReadFrom(extension.GetReader()) as XElement;
                        if (xelement.Name.NamespaceName.Equals(CmisNs.Cmisra) && xelement.Value.Equals(GetCollectionTypeName(collectionType)))
                        {
                            result = collection;
                            break;
                        }
                    }
                }
            }
            return result;
        }
    }
}
